package com.iims.customer.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.customer.domain.Customer;
import com.iims.customer.mapper.CustomerMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:27
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        return baseMapper.selectCustomerList(customer);
    }

    @Override
    public Customer selectOne(Long customerId) {
        return baseMapper.selectCustomerById(customerId);
    }

    @Override
    public String checkCustomerNameUnique(Customer customer) {
        Long customerId = Validator.isNull(customer.getCustomerId()) ? -1L : customer.getCustomerId();
        Customer info = getOne(new LambdaQueryWrapper<Customer>()
                .eq(Customer::getCustomerName, customer.getCustomerName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getCustomerId().longValue() != customerId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertCustomer(Customer customer) {
        return baseMapper.insert(customer);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return baseMapper.updateById(customer);
    }

    @Override
    public int deleteCustomerById(Long customerId) {
        return baseMapper.deleteById(customerId);
    }

    @Override
    public boolean checkCustomerUse(Long customerId) {
        return baseMapper.checkCustomerUseCount(customerId) > 0;
    }

}
